package com.kuang.dao;

import com.kuang.pojo.Department;
import com.kuang.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Repository
public class EmployeeDao {
    //模拟数据库中Employee表中的数据
    private static Map<Integer, Employee> employees = null;

    static {
        employees = new HashMap<Integer, Employee>();

        employees.put(101, new Employee(101, "AA", "A1520559912@qq.com", 0, new Department(101, "教学部")));
        employees.put(102, new Employee(102, "BB", "B1520559912@qq.com", 1, new Department(102, "市场部")));
        employees.put(103, new Employee(103, "CC", "C1520559912@qq.com", 0, new Department(103, "教研部")));
        employees.put(104, new Employee(104, "DD", "D1520559912@qq.com", 1, new Department(104, "运营部")));
        employees.put(105, new Employee(105, "EE", "E1520559912@qq.com", 0, new Department(105, "后勤部")));
    }

    @Autowired
    private DepartmentDao departmentDao;

    //获取全部的员工信息
    public Collection<Employee> getAllEmployees(){
        return employees.values();
    }

    //通过id查询员工信息
    public Employee getEmployeeById(Integer id){
        return employees.get(id);
    }

    //通过id删除员工信息
    public void delEmployeeById(Integer id){
        employees.remove(id);
    }


    private static Integer id = 106;

    //新整员工信息
    public void addEmployee(Employee employee){
        if(employee.getId() == null){
            employee.setId(id++);
        }
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));

        employees.put(employee.getId(),employee);
    }

}
